$(function () {
    // 查询用户名
    $.get("/findAccountId", function (data) {
        if (data != null) {
            $("#span_name").html(data.msg);
        }
    }, "json");

    //查询余额
    $("#selectRemainingBtn").click(function () {
        $.get("/selectRemaining", function (data) {
            $("#selectRemainingSpan").html("<font color='red'>" + data.msg + "</font>");
        });
    });

    //存钱操作
    $("#saveMoneyBtn").click(function () {
        $.post("/saveMoney", {"save_money": $("#save_money").val()}, function (data) {
            $("#saveMoneySpan").html("<font color='red'>" + data.msg + "</font>");
            $("#save_money").val("");
        });
    });

    //取钱操作
    $("#takeMoneyBtn").click(function () {
        $.post("/takeMoney", {"take_money": $("#take_money").val()}, function (data) {
            $("#takeMoneySpan").html("<font color='red'>" + data.msg + "</font>");
            $("#take_money").val("");
        });
    });

    //转账操作
    $("#transactionBtn").click(function () {
        $.post("/transMoney", $("#transactionForm").serialize(), function (data) {
            $("#transactionSpan").html("<font color='red'>" + data.msg + "</font>");
            $("#t_money").val("");
            $("#to_account").val("");
        });
    });

    //查询转账记录操作
    $("#selectTradeBtn").click(function () {
        $.post("/selectTrade", $("#selectTradeForm").serialize(), function (data) {
            if (data.code == 1024) {
                $("#selectTradeDiv").html(data.msg);
            } else if (data.code == 999) {
                var html = "<table border='1' bordercolor='gold' width='100%'><tr><td>账户ID</td><td>操作类型</td><td>操作金额</td><td>操作说明</td></tr>";
                // 如果没有查询出来结果
                $(data.list).each(function (i, n) {
                    var type;
                    console.log(n.tradeType);
                    if (n.tradeType == "0") {
                        type = "存款";
                    } else if (n.tradeType == "1") {
                        type = "取款";
                    } else {
                        type = "转账";
                    }
                    html += "<tr><td>"
                        + n.accountId
                        + "</td><td>"
                        + type
                        + "</td><td>"
                        + n.tradeMoney
                        + "</td><td>"
                        + n.tradeDigest
                        + "</td></tr>"
                });
                html += "</table>";

                $("#selectTradeDiv").html(html);
            }
            $("#startT").val("");
            $("#endT").val("");
        }, "json");
    });
});